﻿Imports System.IO

Public Class FRM_PI

    Dim Decimali As Integer
    Dim DecimaliEstesi As Boolean
    Dim MultiThread As Boolean
    Dim ycrusher_launcher As Process
    Dim ycrusher As Process
    Dim ycrusher_input As StreamWriter
    Dim Tempo As String
    Dim StringaOUT As String
    Dim StringaTEMP As String
    Dim versione As String
    Dim CMB_Decimali_Indice As Integer = 3
    Dim CMB_Thread_Indice As Integer = 1

    Private Sub FRM_PI_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not e.CloseReason = CloseReason.TaskManagerClosing Or e.CloseReason = CloseReason.WindowsShutDown Then
            CMB_Decimali_Indice = CMB_Decimali.SelectedIndex
            CMB_Thread_Indice = CMB_Thread.SelectedIndex
            e.Cancel = True
            Me.Hide()
        End If
    End Sub

    Private Sub FRM_PI_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CMB_Decimali.SelectedIndex = CMB_Decimali_Indice
        CMB_Thread.SelectedIndex = CMB_Thread_Indice
    End Sub

    Private Sub BTN_Bench_Click(sender As System.Object, e As System.EventArgs) Handles BTN_Bench.Click
        Invoke(New Imposta_Stato_BTN_Bench_Delegate(AddressOf Imposta_Stato_BTN_Bench), False)

        Select Case CMB_Decimali.SelectedIndex
            Case 0
                Decimali = 20
                DecimaliEstesi = True
            Case 1
                Decimali = 22
                DecimaliEstesi = True
            Case 2
                Decimali = 24
                DecimaliEstesi = True
            Case 3
                Decimali = 1
                DecimaliEstesi = False
            Case 4
                Decimali = 2
                DecimaliEstesi = False
            Case 5
                Decimali = 3
                DecimaliEstesi = False
        End Select

        If CMB_Thread.SelectedIndex = 1 Then MultiThread = True Else MultiThread = False

        ycrusher_launcher = New Process
        AddHandler ycrusher_launcher.OutputDataReceived, AddressOf ycrusher_launcher_output
        Dim StartInfo As New System.Diagnostics.ProcessStartInfo
        StartInfo.FileName = "y-cruncher\y-cruncher.exe"
        StartInfo.RedirectStandardInput = True
        StartInfo.RedirectStandardOutput = True
        StartInfo.UseShellExecute = False
        StartInfo.CreateNoWindow = True
        ycrusher_launcher.StartInfo = StartInfo
        ycrusher_launcher.Start()
        ycrusher_launcher.BeginOutputReadLine()
    End Sub

    Private Sub ycrusher_launcher_output(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        Try
            If e.Data.StartsWith("Version:") Then
                versione = e.Data.Substring(9).Trim()
                ycrusher = New Process
                AddHandler ycrusher.OutputDataReceived, AddressOf ycrusher_output
                Dim StartInfo As New System.Diagnostics.ProcessStartInfo
                StartInfo.FileName = "y-cruncher\Binaries\" & versione & ".exe"
                StartInfo.Verb = "runas"
                StartInfo.RedirectStandardInput = True
                StartInfo.RedirectStandardOutput = True
                StartInfo.UseShellExecute = False
                StartInfo.CreateNoWindow = True
                If Not Directory.Exists(Application.StartupPath & "\Risultati PI") Then Directory.CreateDirectory(Application.StartupPath & "\Risultati PI")
                StartInfo.WorkingDirectory = Application.StartupPath & "\Risultati PI\"
                ycrusher.StartInfo = StartInfo
                ycrusher.Start()
                ycrusher_input = ycrusher.StandardInput
                ycrusher_input.AutoFlush = True
                ycrusher.BeginOutputReadLine()
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ycrusher_output(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        Try
            StringaOUT = e.Data
            If StringaOUT = "Enter your choice:" Then
                ycrusher_input.WriteLine("0")
            End If
            If StringaOUT = "  1     Multi-Threaded" Then
                If MultiThread Then ycrusher_input.WriteLine("1") Else ycrusher_input.WriteLine("0")
            End If
            If StringaOUT = "  0     I prefer SuperPi sizes... (1M, 2M, 4M...)" Then
                If DecimaliEstesi Then ycrusher_input.WriteLine("0")
                ycrusher_input.WriteLine(Decimali)
            End If
            If StringaOUT.StartsWith("Total Time (with output + verify):") Then
                StringaTEMP = StringaOUT.Substring(34).TrimStart(" ")
                Tempo = StringaTEMP.Substring(0, StringaTEMP.IndexOf(" "))
                Tempo = Tempo.Replace("seconds", "").Trim(" ")
            End If
            If StringaOUT = "Benchmark Successful. The digits appear to be OK." Then
                Try
                    MsgBox(FRM_TempAndStress.STR(58) & Tempo & FRM_TempAndStress.STR(86) & vbCrLf & FRM_TempAndStress.STR(59), MsgBoxStyle.Information, FRM_TempAndStress.STR(60))
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try

                Invoke(New Imposta_Stato_BTN_Bench_Delegate(AddressOf Imposta_Stato_BTN_Bench), True)
            End If
        Catch ex As Exception
        End Try
    End Sub

    Delegate Sub Imposta_Stato_BTN_Bench_Delegate(ByVal stato As Boolean)
    Sub Imposta_Stato_BTN_Bench(ByVal stato As Boolean)
        BTN_Bench.Enabled = stato
    End Sub

End Class